/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.weso.acota.dao;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.traversal.NodeIterator;
import org.weso.acota.to.RequestSuggestionTO;
import org.weso.acota.to.SuggestionTO;
import org.weso.acota.to.TagTO;

/**
 *
 * @author chema
 */
public class TwitterSuggestDAOImpl implements SuggestDAO {

    private String twitterUrl = "http://search.twitter.com/search.json?q=";


    @Override
    public SuggestionTO suggest(RequestSuggestionTO request) {
        NodeIterator it;
        try {
            URL url = new URL(this.twitterUrl + request.getWord());
             HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setInstanceFollowRedirects(true);
            connection.setRequestProperty("Accept", "application/json");
            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK
                    && connection.getContentType().contains("application/json"))
            {
                InputStream is = connection.getInputStream();
                //XML
                String str = slurp(is);
                JSONObject json = new JSONObject(str);
                JSONArray results = json.getJSONArray("results");
                SuggestionTO suggestion = new SuggestionTO();
                List<TagTO> tags = new LinkedList<TagTO>();
                for(int i = 0; i<results.length();i++){
                    JSONObject result =(JSONObject)results.get(i);
                    tags.add(new TagTO(TwitterSuggestDAOImpl.class.getSimpleName(),result.get("text").toString()));
                }
                suggestion.setTags(tags);
                return suggestion;
            } else {
                throw new RuntimeException("Unable to get a representation of the resource");
            }
        } catch (Exception e) {

            throw new RuntimeException(e);
        }
    }


        //Helper Method
         public static List jsonArray2List(JSONArray array) throws JSONException {
            List l = new ArrayList(array.length());
             for (int i = 0; i < array.length(); i++) {
                 if (array.get(i) instanceof JSONArray) {
                     l.add(jsonArray2List((JSONArray) array.get(i)));
                } else {
                     l.add(array.get(i));
                }
            }
            return l;
        }
public static String slurp (InputStream in) throws IOException {
    StringBuffer out = new StringBuffer();
    byte[] b = new byte[4096];
    for (int n; (n = in.read(b)) != -1;) {
        out.append(new String(b, 0, n));
    }
    return out.toString();
}
}

